#define WIN32_LEAN_AND_MEAN
#undef DrawText
#undef LoadImage

#include <GTL.h>
#include <GTLEngine.h>
#include "allegro_backend.h"
#include <GTL/external_libs/tinynet/tinynet.h>

using namespace GTL;
using namespace GTL::Engine;


class Creature : public GameObject
{
	GTL_DEFINE_PRODUCIBLE_OBJECT(Creature);
public:
	//property (Point, Position, Point(400.0f,300.0f), Is::InGroup("Physics"));
	//property (int, Life, 100, Is::InGroup("Stats"), Is::Serialized(), Is::Customized());
	//property (int, Life, 100, Is::InGroup("Stats"), Is::Serialized(), Is::Customized());
	//property (Point, Destination, Point(400.0f, 300.0f), Is::InGroup("Physics"));

	state (Alive)
	{
		event (Enter, ())
		{
			//Parent->SwitchInto("Standing");
			return 0;
		}

		event (Event, (GameEvent* ev))
		{
			printf("Event!\n");
			return 0;
		}
	};
};

class ModeBattle : public Mode
{
public:
	GUI::Button client, server, send_pack;
	Creature* go;

	virtual int Load()
	{
    MainGUI.AddChild(GUI::Control::CreateControlFromWilsonFile("Data/gui.txt"));
		go = Produce<Creature>();
		//printf("Yay: %s\n", go->BitchCount.GetRepresentation<GTL::Formats::Wilson>().c_str());
		
		return 0;
	}

	virtual int Event(GameEvent event) /// TODO: Finish me
	{
		if (event.EventID == GUI::Event::Click && event.EventData.Control == &server)
		{
		} else if (event.EventID == GUI::Event::Click && event.EventData.Control == &client)
		{
		} else if (event.EventID == GUI::Event::Click && event.EventData.Control == &send_pack)
		{
		}
		return 0;
	}

	virtual int Unload()
	{
		return 0;
	}

	virtual int Update(float dt)
	{
		
		return 0;
	}

	virtual int Draw(float dt)
	{
		Backend->ClearScreen(ColorRGBA(0,0,0,1));

    return 0;
	}
};

class ModeLobby : public Mode
{
public:
	Creature* go;


	virtual int Load()
	{
		MainGUI.AddChild(GUI::Control::CreateControlFromWilsonFile("Data/gui.txt"));
		go = Produce<Creature>();
		//printf("Yay: %s\n", go->BitchCount.GetRepresentation<GTL::Formats::Wilson>().c_str());

		return 0;
	}

	virtual int Event(GameEvent event) /// TODO: Finish me
	{
		return 0;
	}

	virtual int Unload()
	{
		return 0;
	}

	virtual int Update(float dt)
	{

		return 0;
	}

	virtual int Draw(float dt)
	{
		Backend->ClearScreen(ColorRGBA(0,0,0,1));

		return 0;
	}
};

class ModeCreateJoin : public Mode
{
public:
	gui (gMain, "Data/gui/cj.wilson");

	//resource (Image, rLogo, "Data/gfx/logo.png");

	//resource (Image, bleh, "Data/button1.png");

	//resource (Font, font, "Data/cour.ttf");

	virtual int Draw(float dt)
	{
		Backend->ClearScreen(ColorRGBA(0,0,0,1));

		return 0;
	}

	virtual int Load()
	{
		return 0;
	}

	virtual int Event(GameEvent event) /// TODO: Finish me
	{
		if (event.EventID == GUI::Event::Click)
		{
			if (event.EventData.Control->Name == "create game")
			{
				NetworkService* ns = (NetworkService*)ParentGame->GetService("Network");
				ns->StartServer(61111);
				//ns->StartConnectingToServer("localhost", 611111);
			}
			if (event.EventData.Control->Name == "join game")
			{
				NetworkService* ns = (NetworkService*)ParentGame->GetService("Network");
				ns->StartConnectingToServer("localhost", 61111);
				//ns->StartConnectingToServer("localhost", 611111);
			}
		} else if (event.EventID == Network::Event::Connected)
		{
			printf("Connected: %i\n", event.EventData.Integer);
		}
		return 0;
	}

	virtual int Unload()
	{
		return 0;
	}

	virtual int Update(float dt)
	{
		return 0;
	}
	
};

class SummonerGame : public GTL::Engine::Game
{
public:
	virtual int StartGame()
	{
		SetNext('conn');
		return 0;
	}
	virtual Mode* CreateMode(int id, int arg1, int arg2)
	{
		switch (id)
		{
		case 'main':
			return AutoMembers::Producible::Produce<ModeBattle>();
		case 'lobb':
			return AutoMembers::Producible::Produce<ModeLobby>();
		case 'conn':
			return AutoMembers::Producible::Produce<ModeCreateJoin>();
		}
		return 0;
	}
private:
	
};

int main()
{
	//Logging::Level = Logging::Trace;
	GTL::Engine::Backend = new AllegroBackend;
	SummonerGame game;
	GTL::Engine::Backend->Init(&game, 1024, 700, false);
	game.Run();

	return 0;
}